//
//  WQVisitorViewController.swift
//  WeiBo
//
//  Created by 张德华 on 7/23/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQVisitorViewController: UIViewController {

    /**访客View*/
    var visitorView: WQVisitorView?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()


    }
    
    override func loadView() {
        
        if WQUserAccountModel.sharedUserAccount.isLogin {
            loadData()
        } else {
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(WQVisitorViewController.loginSuccessed), name: WQWeiBoLoginSuccessedNotification, object: nil)
            
            
        }
        
        
        WQUserAccountModel.sharedUserAccount.isLogin ?  view = tableView : setupVisitor()
        
        setupNav()
    }
    
    // MARK: - ************登录成功监听方法
    @objc private func loginSuccessed() {
        
        view = nil
        NSNotificationCenter.defaultCenter().removeObserver(self, name: WQWeiBoLoginSuccessedNotification, object: nil)
        
    }
    
    // MARK: - ************加载数据
    func loadData() {
        
    }
    
    
    // MARK: - ************访客视图导航栏设置
    func setupNav() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(WQVisitorViewController.leftButtonClicked))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(WQVisitorViewController.rightButtonClicked))
    }
    
    // MARK: - ************访客界面
    func setupVisitor() {
        
        visitorView = WQVisitorView()
        view = visitorView
        
        ///------ 上级->visitorView
        visitorView!.visitorClosure = {
            print("visitorVc->登录按钮")
            
            let oAuthVc = WQOAuthViewController()
            let oAuthNav = UINavigationController(rootViewController: oAuthVc)
            
            self.presentViewController(oAuthNav, animated: true, completion: nil)
        }
        
    }
    
    
    // MARK: - ************导航按钮监听事件
    func leftButtonClicked() {
        print("visitorNavLeft->注册按钮")
    }
    func rightButtonClicked() {
        print("visitorNavRight->登录按钮")
    }
    
    
    // MARK: - ************懒加载
    lazy var tableView = UITableView()

}
